<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Chat Page</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            height: 100vh;
        }

        #chat-box {
            flex: 7;
            border: 1px solid #ccc;
            overflow-y: scroll;
            padding: 10px;
            width: 60%;
        }

        .message {
            padding: 5px;
            margin: 5px;
        }

        .user-message {
            background-color: #dcf8c6;
            text-align: right;
        }

        .system-message {
            background-color: #e6e6e6;
        }

        form {
            display: flex;
            flex-wrap: nowrap;
            padding: 10px;
            background-color: #f5f5f5;
            position: fixed;
            bottom: calc(10vh - 50px);
            width: 60%;
            box-sizing: border-box;
        }

        input[type="text"] {
            flex: 1;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            margin-right: 10px;
            min-height: 10px;
            max-height: 200px;
            height: auto;
            overflow-y: auto;
            resize: vertical;
            word-wrap: break-word;
            overflow-wrap: break-word;
            white-space: pre-wrap;
        }

        button {
            padding: 5px 10px;
            border: none;
            border-radius: 5px;
            background-color: #4caf50;
            color: white;
            cursor: pointer;
        }
    </style>
</head>

<body>
<div id="chat-box"></div>
<form id="chat-form">
    <input type="text" id="user-input" placeholder="Type your message...">
    <button type="submit">Send</button>
</form>
<script>
    document.getElementById('chat-form').addEventListener('submit', function (event) {
        event.preventDefault();
        const userInput = document.getElementById('user-input').value;
        if (userInput.trim() === '') return;
        addMessage(userInput, 'user-message');
        fetch('http://localhost:8080/answer/chat', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ chatMessage: userInput })
        })
            .then(response => response.json())
            .then(data => {
                addMessage(data.answer,'system-message');
            })
            .catch(error => console.error('Error:', error));
        document.getElementById('user-input').value = '';
    });

    function addMessage(message, className) {
        const chatBox = document.getElementById('chat-box');
        const messageDiv = document.createElement('div');
        messageDiv.classList.add('message', className);
        messageDiv.textContent = message;
        chatBox.appendChild(messageDiv);
        chatBox.scrollTop = chatBox.scrollHeight;
    }
</script>
</body>

</html>